package com.jushu.storbox.view.dialog;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.jushu.storbox.R;
import com.jushu.storbox.actions.FileAction;
import com.jushu.storbox.actions.FolderAction;
import com.jushu.storbox.entities.FileFolderInfo;
import com.jushu.storbox.entities.FileType;
import com.jushu.storbox.util.NameUtils;
import com.jushu.storbox.util.PublicTools;

public class RenameDialog {

	 public void rename(final Activity activity, final FileFolderInfo fileInformation,
			 final ProgressDialog loading_ll, final Handler msgHandler)
	    {
	        final String oldName = fileInformation.getName();
	        
	        final ClouddriveDialog rename = new ClouddriveDialog(activity, R.style.dialog_upload,
	            R.layout.window_dialog_create_folder);	 
	        WindowManager windowManager=activity.getWindowManager();
	        Display display=windowManager.getDefaultDisplay();
	        rename.setWidth(display.getWidth());
	        rename.setPosition(Gravity.CENTER_HORIZONTAL, Gravity.CENTER_VERTICAL);
	        View conventView = rename.getConventView();
	        Button confirm_btn = (Button) conventView.findViewById(R.id.confirm_btn);
	        Button cancle_btn = (Button) conventView.findViewById(R.id.cancle_btn);
	        TextView tittleText = (TextView) conventView.findViewById(R.id.tittle_tv);
	        final EditText rename_et = (EditText) conventView.findViewById(R.id.inputText_et);
	        rename_et.setText(oldName);
	        rename_et.selectAll();
	        tittleText.setText(activity.getString(R.string.rename_title));
	        
	        cancle_btn.setOnClickListener(new View.OnClickListener()
	        {
	            @Override
	            public void onClick(View v)
	            {
	                rename.dismiss();
	                if (loading_ll.isShowing())
	                {
	                    loading_ll.dismiss();
	                }
	            }
	        });
	        confirm_btn.setOnClickListener(new View.OnClickListener()
	        {
	            @Override
	            public void onClick(View v)
	            {
	                if (!loading_ll.isShowing())
	                {
	                    loading_ll.show();
	                }
	                rename.dismiss();
	                final String newName = rename_et.getText().toString().trim();
	                if (NameUtils.isValidFileName(newName))
	                {
	                    if (newName.equals(oldName))
	                    {
	                        Bundle bundle = new Bundle();
	                        bundle.putString("folderID", fileInformation.getParent());
	                        Toast.makeText(activity,
	                            activity.getString(R.string.rename_filefolder_nochange),
	                            Toast.LENGTH_SHORT).show();
	                        if (loading_ll.isShowing())
	                        {
	                        	loading_ll.dismiss();
	                        }
	                    }
	                    else
	                    {
	                    	if (fileInformation.getIsFile() == FileType.File.value())
                            {
	                    		new FileAction().renameFile(newName, fileInformation, activity, msgHandler);
                            }else{
                            	new FolderAction().renameFolder(newName, fileInformation, activity, msgHandler);
                            }
	                    }
	                }
	                else
	                {
	                	Toast.makeText(activity, activity.getString(R.string.allfile_file_name_not_standard), Toast.LENGTH_SHORT)
                        .show();
	                	rename.dismiss();
	  	                if (loading_ll.isShowing())
	  	                {
	  	                    loading_ll.dismiss();
	  	                }
	                }
	                
	            }
	        });
	        rename.show();
	        PublicTools.showKey(rename_et, activity);
	    }
}
